package com.ewzj.bigdata.mvp.ui.activity

import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.view.ViewPager
import android.widget.Toast

import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.extensions.DelegatesExt
import com.ewzj.bigdata.di.component.DaggerMainComponent
import com.ewzj.bigdata.di.module.MainModule
import com.ewzj.bigdata.mvp.contract.MainContract
import com.ewzj.bigdata.mvp.presenter.MainPresenter
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.DataHelper
import com.jess.arms.utils.DeviceUtils
import kotlinx.android.synthetic.main.activity_main_tab.*
import org.simple.eventbus.EventBus
import java.util.*

class MainTabActivity : BaseActivity<MainPresenter>() ,MainContract.View{
    private var isQuit: Boolean? = false
    private val timer = Timer()
    private var versioncode:Int by DelegatesExt.preference(this,AppKey.CUR_VERSION,0)
    override fun initCityName(city: String) {

    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerMainComponent.builder().appComponent(appComponent)
                .mainModule(MainModule(this))
                .build().inject(this)
    }

    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_main_tab
    }

    override fun useFragment(): Boolean {
        return true
    }
    override fun initData(savedInstanceState: Bundle?) {
        mPresenter.initViewPager(vpcontent)
        rgmain.setOnCheckedChangeListener { group, checkedId ->
            run {
                when (checkedId) {
                    R.id.rbhome -> vpcontent.currentItem = 0
                    R.id.rbnews -> vpcontent.currentItem = 1
                    R.id.rbvideo -> vpcontent.currentItem = 2
                    R.id.rbmine -> vpcontent.currentItem = 3
                }
            }
        }
        vpcontent.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                mPresenter.changeStatusbar(position)
            }
        })
//        mPresenter.initBdLocation()
        showUpdateWindow()

    }

    override fun showLoading() {

    }

    override fun launchActivity(intent: Intent?) {

    }

    override fun hideLoading() {

    }

    override fun killMyself() {

    }

    override fun showMessage(message: String?) {

    }

    override fun onBackPressed() {
        if (isQuit === false) {
            isQuit = true
            Toast.makeText(baseContext, "再按一次返回键退出程序", Toast.LENGTH_SHORT).show()
            var task: TimerTask? = null
            task = object : TimerTask() {
                override fun run() {
                    isQuit = false
                }
            }
            timer.schedule(task, 2000)
        } else {
            finish()
            System.exit(0)
            android.os.Process.killProcess(android.os.Process.myPid())
        }
    }

    private fun showUpdateWindow() {
        if (!AppKey.mappConfig.download_url.isNullOrEmpty()) {
            if (AppKey.mappConfig.getVersion_code() > DeviceUtils.getVersionCode(this) && versioncode!= AppKey.mappConfig.getVersion_code()) {
                val builder = AlertDialog.Builder(this)
                builder.setMessage(AppKey.mappConfig.getContent())
                builder.setTitle("版本更新")
                builder.setPositiveButton("确认") { dialog, which ->
                    dialog.dismiss()
                    val intent = Intent()
                    intent.action = "android.intent.action.VIEW"
                    val content_url = Uri.parse(AppKey.mappConfig.getDownload_url())
                    intent.data = content_url
                    startActivity(intent)
                    dialog.dismiss()
                }
                builder.setNegativeButton("忽略更新") { dialog, which ->
                    versioncode=AppKey.mappConfig.version_code
                    dialog.dismiss()
                }
                val dialog = builder.create()
                dialog.setCanceledOnTouchOutside(false)
                dialog.show()
            }
        }
    }
}
